package com.ren.bottomnavigationview.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.ren.bottomnavigationview.fragment.BooksFragment;
import com.ren.bottomnavigationview.view.BottomNavigationViewHelper;
import com.ren.bottomnavigationview.fragment.DiscoverFragment;
import com.ren.bottomnavigationview.fragment.MoreFragment;
import com.ren.bottomnavigationview.fragment.NewsFragment;
import com.ren.bottomnavigationview.R;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView mBottomNaView;

    private NewsFragment newsFragment;
    private BooksFragment booksFragment;
    private DiscoverFragment discoverFragment;
    private MoreFragment moreFragment;
    private FragmentManager fragmentManager = getSupportFragmentManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBottomNaView = (BottomNavigationView) findViewById(R.id.bottom_view);
        BottomNavigationViewHelper.disableShiftMode(mBottomNaView);
        newsFragment = new NewsFragment();
        booksFragment = new BooksFragment();
        discoverFragment = new DiscoverFragment();
        moreFragment = new MoreFragment();

        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_content, newsFragment);
        transaction.add(R.id.fl_content, booksFragment).hide(booksFragment);
        transaction.add(R.id.fl_content, discoverFragment).hide(discoverFragment);
        transaction.add(R.id.fl_content, moreFragment).hide(moreFragment);
        transaction.commit();

        mBottomNaView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setChecked(true);
                switch (item.getItemId()) {
                    case R.id.item_news:
                        fragmentManager.beginTransaction()
                                .hide(booksFragment)
                                .hide(discoverFragment)
                                .hide(moreFragment)
                                .show(newsFragment)
                                .commit();
                        break;
                    case R.id.item_lib:
                        fragmentManager.beginTransaction()
                                .hide(newsFragment)
                                .hide(discoverFragment)
                                .hide(moreFragment)
                                .show(booksFragment)
                                .commit();
                        break;
                    case R.id.item_find:
                        fragmentManager.beginTransaction()
                                .hide(newsFragment)
                                .hide(booksFragment)
                                .hide(moreFragment)
                                .show(discoverFragment)
                                .commit();
                        break;
                    case R.id.item_more:
                        fragmentManager.beginTransaction()
                                .hide(booksFragment)
                                .hide(discoverFragment)
                                .hide(newsFragment)
                                .show(moreFragment)
                                .commit();
                        break;
                }
                return false;
            }
        });
    }
}
